﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DGSFramework;
using System.Web.Security;

namespace Framework
{
    public partial class Login : System.Web.UI.Page
    {
        public DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public udv_Users udvUsers = new udv_Users();

        protected void Page_Load(object sender, EventArgs e)
        {
            tbMemberPassword.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+btnLoginMember.UniqueID+"').click();return false;}} else {return true}; ");
        }
        protected void btnLoginMember_Click(object sender, EventArgs e)
        {
            try
            {
                //check member
                string mem = tbMember.Text;
                string pass = tbMemberPassword.Text;
                var getmem = entity.udv_Users.Where(a => a.UserName == mem && (a.IsActive == true) && (a.ExpiredDate > DateTime.Now)).FirstOrDefault();

                if (getmem != null && Membership.ValidateUser(mem, pass))
                {
                    FormsAuthentication.SetAuthCookie(mem, true);              
                    //Session["objMember"] = getmem;
                    var returnUrl = string.Empty;
                    if (Request.QueryString["url"] != null)
                        returnUrl = Request.QueryString["url"].ToString();
                    if (!string.IsNullOrEmpty(returnUrl))
                        Response.Redirect(returnUrl);
                }else
                {
                    lblAlertMember.Text = "Vui Lòng Kiểm Tra Member ID và Password !";
                }               
                            
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        
    }
}